package com.atlas.micro.trafico;

import java.io.IOException;
import javax.microedition.lcdui.*;

/**
 * <p>Title: TraficoMadrid</p>
 *
 * <p>Description: Programa j2me para ver las cámaras de Tráfico de Madrid</p>
 *
 * <p>Copyright: Copyright (c) Antonio García Rodríguez 2005</p>
 *
 * <p>Company: </p>
 *
 * @author Antonio García Rodríguez
 * @version 1.0
 */
public class ImageScreenCanvas extends Canvas implements CommandListener {
        /**
         * Imagen a mostrar
         */
        private Image image = null;
        private String mensaje = null;

    public ImageScreenCanvas() {
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }

    }

    private void jbInit() throws Exception {
        this.mensaje ="Cargando...";
        this.setFullScreenMode(true);
        new Thread(){
            public void run(){
                try {
                    String path = "/resources/splash.jpg";
                    image = Image.createImage(path);
                    repaint();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
        
        
    }

    public void commandAction(Command command, Displayable displayable) {
        /** @todo Add command handling code */

    }

    protected void paint(Graphics g) {
           
        
        if(image!=null && image.getWidth()>0 && image.getHeight()>0){
            g.setColor(0x0);
            g.fillRect(0,0,this.getWidth(),this.getHeight());
            g.drawImage(TraficoMovilManager.resize(image, this.getWidth(), this.getHeight(), false,true), this.getWidth()/2, this.getHeight()/2, Graphics.VCENTER|Graphics.HCENTER);            
        }else{
             g.setColor(0xffffff);
            g.fillRect(0,0,this.getWidth(),this.getHeight());
            g.setColor(0x0);
            g.drawString(mensaje, this.getWidth()/2, this.getHeight()/2, Graphics.TOP|Graphics.HCENTER);            
        }
    }

    /**
     * keyPressed
     *
     * @param keyCode int
     * @todo Implement this javax.microedition.lcdui.Canvas method
     */
    protected void keyPressed(int keyCode) {
        TraficoMovilApp.instance.showTraficoMovilPrincipal();
    }

    public Image getImage() {
        return image;
    }

    public String getMensaje() {
        return mensaje;
    }

    public void setImage(Image image) {
        this.image = null;
        this.image = image;
    }

    public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
        this.repaint();
    }

}
